package JDK8_Date;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Set;

public class Date_JDK8 {
    public static void main(String[] args) {
//        zoneId();
//        instant();
//        zoneDateTime();
//        dateTimeFormatter();
//        localDate();
        TimeUtil();
    }

    public static void zoneId() {
//        获取所有时区的名称
        Set<String> availableZoneIds = ZoneId.getAvailableZoneIds();
        System.out.println(availableZoneIds);
//        获取系统时区
        ZoneId systemDefault = ZoneId.systemDefault();
        System.out.println(systemDefault);
//        获取指定的时区
        ZoneId zoneId = ZoneId.of("Asia/Shanghai");
        System.out.println(zoneId);
    }

    public static void instant() {
//        获取当前世界标准时间
        Instant now = Instant.now();
        System.out.println(now);
//        获取指定时区的标准时间
        ZonedDateTime zonedDateTime = Instant.now().atZone(ZoneId.of("Asia/Shanghai"));
        System.out.println(zonedDateTime);
//          获取距离1970 1000ms后的时间戳
        Instant milli = Instant.ofEpochMilli(1000L);
        System.out.println(milli);
//        获取距离1970 2s后的时间戳
        Instant second = Instant.ofEpochSecond(2L);
        System.out.println(second);
//        isXXX判断方法
        System.out.println(milli.isBefore(second));
        System.out.println(milli.isAfter(second));
//        加减时间方法
        System.out.println(milli.plusMillis(1000L));
        System.out.println(second.minusSeconds(1L));
    }

    public static void zoneDateTime() {
//        now() 获取当前时区的标准时间
        ZonedDateTime now = ZonedDateTime.now();
        System.out.println(now);
//       of(args) 获取指定时间
        ZonedDateTime zonedDateTime = ZonedDateTime.of(2023, 6, 19, 20, 06,
                0, 0, ZoneId.of("Asia/Shanghai"));
        System.out.println(zonedDateTime);
//       修改时间指定值
        ZonedDateTime zonedDateTime1 = zonedDateTime.withYear(2022);
        System.out.println(zonedDateTime1);
//        加减 之间指定值
        ZonedDateTime zonedDateTime2 = zonedDateTime1.plusYears(1);
        System.out.println(zonedDateTime2);
        ZonedDateTime zonedDateTime3 = zonedDateTime2.minusYears(1);
        System.out.println(zonedDateTime3);

    }

    public static void dateTimeFormatter() {
//        格式化时间对象
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        ZonedDateTime now = ZonedDateTime.now();
        String format = dateTimeFormatter.format(now);
        System.out.println(format);
    }

    public static void localDate() {
//        获取当前 年月日
        LocalDate now = LocalDate.now();
        System.out.println(now);
//        获取指定 年月日
        System.out.println(LocalDate.of(2023, 6, 19));
//        获取指定值
        System.out.println(now.getYear());
//        获取当前 时分秒
        LocalTime localTime = LocalTime.now();
        System.out.println(localTime);
//        获取 时分秒
        System.out.println(LocalTime.of(20, 37, 00));
    }

    public static void TimeUtil() {
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime birth = LocalDateTime.of(2002, 9, 5, 0, 0, 0, 0);
        Duration between = Duration.between(birth, now);
        System.out.println(between.toDays());

        LocalDate now1 = LocalDate.now();
        LocalDate birth1 = LocalDate.of(2002, 9, 5);
        Period between1 = Period.between(birth1, now1);
        System.out.println(between1.toTotalMonths());
        long between2 = ChronoUnit.YEARS.between(birth, now);
        System.out.println(between2);

    }
}
